#include "IStateMachine.h"
#include <string.h>

int CStateMachineCtx::set(int slotId,const char* slotName,const char* slotInfo)
{
    strncpy(m_szSlotName[slotId],slotName,sizeof(m_szSlotName[slotId])-1);
    m_szSlotName[slotId][sizeof(m_szSlotName[slotId])-1] = '\0';
    strncpy(m_szSlotInfo[slotId],slotInfo,sizeof(m_szSlotInfo[slotId])-1);
    m_szSlotInfo[slotId][sizeof(m_szSlotInfo[slotId])-1] = '\0';
}

const char* CStateMachineCtx::get(int slotId)
{
    return m_szSlotInfo[slotId];
}


CState::CState(const char* pName)
{
    strncpy(m_szName,pName,sizeof(m_szName)-1);
    m_szName[sizeof(m_szName)-1] = '\0';
}

CState::~CState()
{
    
}

CStateMachine::CStateMachine(const char* pName)
{
    strncpy(m_szName,pName,sizeof(m_szName)-1);
    m_szName[sizeof(m_szName)-1] = '\0';
    m_statIdx = 0;
    m_statNum = 0;
}

int CStateMachine::registerState(CState* pState)
{
    if(m_statNum >= MAX_STATENUM_IN_STATEMACHINE)
    {
        return -1;
    }
    m_pState[m_statNum] = pState;
    m_statNum++;
    return m_statNum;
}

int CStateMachine::onMessage(CPushStateMsg& msg)
{
    int ret = m_pState[m_statIdx]->pushState(msg,m_ctx);
    if(ret < 0 || ret >= m_statNum)
    {
        return -1;
    }
    m_statIdx = ret;
    return 0;
}

